package com.hilda.twitter.parser.utils;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

public class HttpGetAsyncTask extends AsyncTask<String, Void, String> {
	public static final int NO_CONNECTION = -1;

	private UrlCallBack mUrlCallBack;
	private HttpGet mHttpGet;
	private DefaultHttpClient mDefaultHttpClient;

	public HttpGetAsyncTask(UrlCallBack urlCallBack) {
		mUrlCallBack = urlCallBack;
		mDefaultHttpClient = new DefaultHttpClient();
	}

	@Override
	protected void onPreExecute() {

		super.onPreExecute();
	}

	@Override
	protected String doInBackground(String... params) {
		int responseCode = HttpStatus.SC_NOT_FOUND;
		HttpResponse httpResponse = null;
		HttpEntity entity = null;
		String content = null;
 
		
		try {		
			mHttpGet = new HttpGet(params[0]);
			httpResponse = mDefaultHttpClient.execute(mHttpGet);
			responseCode = httpResponse.getStatusLine().getStatusCode();
			
			if (responseCode == HttpStatus.SC_OK) {
				entity = httpResponse.getEntity();
				content = EntityUtils.toString(entity);
			} else {
				Log.e("crawDroid", ".HttpGetAsyncTask() response code: "
						+ responseCode);
			}
		} catch (ClientProtocolException e) {
			Log.e("crawDroid", ".HttpGetAsyncTask(): " + e.getMessage());
		} catch (IOException e) {
			Log.e("crawDroid", ".HttpGetAsyncTask(): " + e.getMessage());
		} catch (ParseException e) {
			Log.e("crawDroid", ".HttpGetAsyncTask(): " + e.getMessage());
		}

		return content;
	}

	@Override
	protected void onPostExecute(String httpContent) {

		mUrlCallBack.onFinish(httpContent);
		super.onPostExecute(null);
	}

}
